package com.canbot.u05.utils;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;

import java.io.File;
import java.util.List;

public class ActivityUtil {
//	public static String getTopActivity(Context context) {
//		ActivityManager am = (ActivityManager) context
//				.getSystemService(Context.ACTIVITY_SERVICE);
//		List<RunningTaskInfo> runningTaskInfos = am.getRunningTasks(1);
//		if (runningTaskInfos != null) {
//			return (runningTaskInfos.get(0).topActivity).toString();
//		} else
//			return null;
//	}

        public static String getTopActivityName(Context context) {
                String topActivityClassName = null;
                ActivityManager activityManager = (ActivityManager) (context
                        .getSystemService(android.content.Context.ACTIVITY_SERVICE));
                List<RunningTaskInfo> runningTaskInfos = activityManager
                        .getRunningTasks(1);
                if (runningTaskInfos != null) {
                        ComponentName f = runningTaskInfos.get(0).topActivity;
                        topActivityClassName = f.getClassName();
                }
                return topActivityClassName;
        }

        public static void install(Context context, File file) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setAction(android.content.Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(file),
                        "application/vnd.android.package-archive");
                context.startActivity(intent);
        }

        public static int getAppVersionCode(Context context) {
                try {
                        PackageInfo packageInfo = context.getPackageManager()
                                .getPackageInfo(context.getPackageName(), 0);
                        return packageInfo.versionCode;
                }
                catch (Exception e) {
                        e.printStackTrace();
                }

                return -1;
        }

        public static int getAppVersionCode(Context context, String packageName) {
                try {
                        PackageInfo packageInfo = context.getPackageManager()
                                .getPackageInfo(packageName, 0);
                        return packageInfo.versionCode;
                }
                catch (Exception e) {
                        e.printStackTrace();
                }

                return -1;
        }


        public static String getApkPath(Context context, String packageName) {
                try {
                        ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_META_DATA);
                        String path = applicationInfo.sourceDir;
                        return path;
                }
                catch (PackageManager.NameNotFoundException e) {
                        e.printStackTrace();
                }
                return null;
        }
}
